package top.vanchor.myblog2back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import top.vanchor.myblog2back.entity.BlogUser;
import top.vanchor.myblog2back.mapper.BlogSubscribeMapper;
import top.vanchor.myblog2back.mapper.BlogUserMapper;
import top.vanchor.myblog2back.service.IBlogUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2021-11-12
 */
@Service
public class BlogUserServiceImpl extends ServiceImpl<BlogUserMapper, BlogUser> implements IBlogUserService {
    @Resource
    private BlogUserMapper blogUserMapper;

    @Override
    public BlogUser toLogin(String username, String password) {
        QueryWrapper<BlogUser> wrapper = new QueryWrapper<>();
        wrapper.eq("u_tel",username);
        wrapper.eq("u_password",password);
        return blogUserMapper.selectOne(wrapper);
    }

    @Override
    public int toModifyUserInfo(BlogUser blogUser) {
        UpdateWrapper<BlogUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("u_id", blogUser.getUId());
        return blogUserMapper.update(blogUser, wrapper);
    }

    @Override
    public BlogUser getUserById(Integer uId) {
        //根据id获取userBean
        QueryWrapper<BlogUser> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",uId);
        return blogUserMapper.selectOne(wrapper);
    }

    @Override
    public boolean isPhoneNumUnique(String phoneNum) {
        QueryWrapper<BlogUser> wrapper = new QueryWrapper<>();;
        wrapper.eq("u_tel",phoneNum);
        return blogUserMapper.isPhoneNumUnique(phoneNum)>0 ? false: true;
    }

    @Override
    public int userRegister(Map<String, Object> data) {
        BlogUser user = new BlogUser();
        user.setUPassword(String.valueOf(data.get("password")));
        user.setUTel(String.valueOf(data.get("phoneNum")));
        user.setUName("");
        return blogUserMapper.insert(user);
    }

    @Override
    public BlogUser getUserInfoById(Integer uId,Integer loginUserId) {
        return blogUserMapper.getUserInfoById(uId,loginUserId);
    }

    @Override
    public List<BlogUser> getFriendListById(Integer id) {
        return blogUserMapper.getFriendListById(id);
    }

    @Override
    public PageInfo<BlogUser> searchUsers(String searchText, Integer id, Integer index, Integer num) {
        PageHelper.startPage(index,num);
        return new PageInfo<>(blogUserMapper.searchUsers(searchText,id));
    }

    @Override
    public int changeStatusByUser(BlogUser user, int day) {
        if ("0".equals(user.getUStatus())){
            //已被封禁，不能再封禁
            return -1;
        }
        long endTime = System.currentTimeMillis()/1000 + 60L *60*24*day;
        user.setUBanEndTime(String.valueOf(endTime));
        user.setUStatus("0");
        QueryWrapper<BlogUser> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",user.getUId());
        return blogUserMapper.update(user,wrapper);
    }
}
